package com.genesis.yunnanji.view;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.MessageEventBean;
import com.genesis.yunnanji.utils.RegexUtils;
import com.youth.xframe.widget.XStateButton;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedHashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建日期：2018/9/21 on 10:14
 * 作者:Kite
 * 描述:绑定界面
 */
public class BindingActivity extends BaseActivity {

    @BindView(R.id.et_info)
    EditText etInfo;

    @BindView(R.id.et_code)
    EditText etCode;

    @BindView(R.id.tv_get_code)
    TextView tvGetCode;

    @BindView(R.id.btn_next)
    XStateButton btnNext;

    private LinkedHashMap<Integer, String> errorMap = new LinkedHashMap<>();

    private int mPassInfo;//界面传递数据：0绑定手机，1绑定邮箱，2修改手机，3修改邮箱，4修改密码

    @Override
    public int getLayoutId() {
        return R.layout.activity_binding;
    }

    @Override
    public void initView() {
        super.initView();
        Bundle bundle = null;
        if (bundle == null) {
            mPassInfo = getIntent().getIntExtra("p0", 0);
            String p1 = getIntent().getStringExtra("p1");
            InputFilter[] filters = {new InputFilter.LengthFilter(11)};
            switch (mPassInfo) {
                case 0:
                    setTitle("绑定手机");
                    etInfo.setInputType(InputType.TYPE_CLASS_NUMBER);
                    etInfo.setFilters(filters);
                    break;
                case 1:
                    setTitle("绑定邮箱");
                    etInfo.setHint("请输入邮箱");
                    etInfo.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
                    break;
                case 2:
                    setTitle("修改绑定手机");
                    etInfo.setInputType(InputType.TYPE_CLASS_NUMBER);
                    etInfo.setFilters(filters);
                    etInfo.setText(p1);
                    etInfo.setSelection(etInfo.getText().length());
                    break;
                case 3:
                    setTitle("修改绑定邮箱");
                    etInfo.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
                    etInfo.setText(p1);
                    etInfo.setSelection(etInfo.getText().length());
                    break;
                case 4:
                    setTitle("修改密码");
                    etInfo.setInputType(InputType.TYPE_CLASS_NUMBER);
                    etInfo.setFilters(filters);
                    etInfo.setText(p1);
                    etInfo.setCursorVisible(false);
                    etInfo.setFocusable(false);
                    etInfo.setFocusableInTouchMode(false);
                    etInfo.setSelection(etInfo.getText().length());
                    break;
            }
        }
        editIsEmpty();
    }

    @OnClick({R.id.tv_get_code, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:
                if (mPassInfo == 0 || mPassInfo == 2 || mPassInfo == 4) {
                    getCode(etInfo, tvGetCode, true,"");
                } else {
                    getCode(etInfo, tvGetCode, false,"");
                }
                break;
            case R.id.btn_next:
                if (mPassInfo == 4) {
                    advance(SetNewPasswordActivity.class);
                } else {
                    advance(IsBindingSuccessActivity.class, mPassInfo);
                    EventBus.getDefault().post(new MessageEventBean(mPassInfo, etInfo.getText().toString()));
                }
                break;
        }
    }

    private void editIsEmpty() {
        TextWatcher etInfoWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null) {
                    String mobile = editable.toString().replace(" ", "");
                    if (mPassInfo == 0 || mPassInfo == 2 || mPassInfo == 4) {
                        if (!TextUtils.isEmpty(mobile)) {
                            errorMap.remove(1);
                            if (mobile.length() == 11 && RegexUtils.checkMobile(mobile)) {
                                errorMap.remove(2);
                            } else {
                                errorMap.put(2, "请填写正确的手机号码");
                            }
                        } else {
                            errorMap.put(1, "请输入您的手机号码");
                        }
                    } else {
                        if (!TextUtils.isEmpty(mobile)) {
                            errorMap.remove(1);
                            if (mobile.length() > 7 && RegexUtils.checkEmail(mobile)) {
                                errorMap.remove(2);
                            } else {
                                errorMap.put(2, "请填写正确的邮箱");
                            }
                        } else {
                            errorMap.put(1, "请输入您的邮箱");
                        }
                    }
                    changeClickable();
                }
            }
        };
        etInfo.addTextChangedListener(etInfoWatcher);
        TextWatcher codeWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null) {
                    String code = editable.toString();
                    if (!TextUtils.isEmpty(code)) {
                        errorMap.remove(3);
                        if (code.length() >= 4 && code.length() <= 6) {
                            errorMap.remove(4);
                        } else {
                            errorMap.put(4, "请输入正确的验证码");
                        }
                    } else {
                        errorMap.put(3, "请输入验证码");
                    }
                    changeClickable();
                }
            }
        };
        etCode.addTextChangedListener(codeWatcher);
        if (mPassInfo == 0) {
            errorMap.put(1, "请输入您的手机号码");
        } else if (mPassInfo == 0) {
            errorMap.put(1, "请输入您的邮箱");
        }
        errorMap.put(3, "请输入您的验证码");
    }

    public void changeClickable() {
        if (errorMap.isEmpty()) {
            btnNext.setClickable(true);
            btnNext.setNormalBackgroundColor(getResources().getColor(R.color.colorTheme));
        } else {
            btnNext.setClickable(false);
            btnNext.setNormalBackgroundColor(getResources().getColor(R.color.color_ccc));
        }
    }

}
